//
//  YQRadioPlayViewController.m
//  EPA--1
//
//  Created by lanou3g on 16/4/21.
//  Copyright © 2016年 刘永庆. All rights reserved.
//

#import "YQRadioPlayViewController.h"
#import "YQRadioListModel.h"
#import "YQAVManager.h"
@interface YQRadioPlayViewController ()
@property (weak, nonatomic) IBOutlet UILabel *titleLable;
@property (weak, nonatomic) IBOutlet UIImageView *imageView1;
@property (weak, nonatomic) IBOutlet UILabel *currentLable;
@property (weak, nonatomic) IBOutlet UILabel *totalLable;
@property (weak, nonatomic) IBOutlet UISlider *rateSlider;
@property (nonatomic, strong) NSTimer *timer;
@property (weak, nonatomic) IBOutlet UIButton *playButton;

@end

@implementation YQRadioPlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.translucent = NO;
    
    YQRadioListModel *model = self.radioArray[self.currentIndex];
    self.titleLable.text = model.title;
    [self.imageView1 sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    [YQAVManager shareManger].currentIndex = self.currentIndex;
    [YQAVManager shareManger].itemArray = [NSMutableArray new];
    NSMutableArray *tempArray = [NSMutableArray new];
    for (YQRadioListModel *model in self.radioArray) {
        NSURL *url = [NSURL URLWithString:model.musicUrl];
        AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
        [tempArray addObject:item];
    }
    [YQAVManager shareManger].itemArray = tempArray;
    [[YQAVManager shareManger] play];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
    self.rateSlider.value = 0;
}
- (void)timeAction
{
    float current = [YQAVManager shareManger].player.currentTime.value / [YQAVManager shareManger].player.currentTime.timescale;
    NSString *currentStr = [NSString stringWithFormat:@"%02ld:%02ld",(NSInteger)current / 60,(NSInteger)current % 60];
    self.currentLable.text = currentStr;
    float total = 0;
    float rate = 0;
    if ([YQAVManager shareManger].player.currentItem.duration.timescale > 0) {
        total  = [YQAVManager shareManger].player.currentItem.duration.value / [YQAVManager shareManger].player.currentItem.duration.timescale;
        rate = current / total;
    }
   
    NSString *totalStr = [NSString stringWithFormat:@"%02ld:%02ld",(NSInteger)total / 60,(NSInteger)total % 60];
    self.totalLable.text = totalStr;
    
    
    [self.rateSlider setValue:rate animated:YES];
}

- (IBAction)didClickpreviousButton:(UIButton *)sender {
    [[YQAVManager shareManger] previous];
    self.currentIndex--;
    if (self.currentIndex < 0) {
        self.currentIndex = self.radioArray.count - 1;
    }
    YQRadioListModel *model = self.radioArray[self.currentIndex];
    [self.imageView1 sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    [ self.playButton setImage:[UIImage imageNamed:@"播放.jpg"] forState:UIControlStateNormal];
    
}
- (IBAction)didClickPlayButton:(UIButton *)sender {
    if ([YQAVManager shareManger].player.rate == 0) {
        [[YQAVManager shareManger] play];
        [(UIButton *)sender setImage:[UIImage imageNamed:@"播放.jpg"] forState:UIControlStateNormal];
    }else {
        [[YQAVManager shareManger] pause];
        [(UIButton *)sender setImage:[UIImage imageNamed:@"暂停.jpg"] forState:UIControlStateNormal];
    }
}
- (IBAction)didClickNextButton:(UIButton *)sender {
    [[YQAVManager shareManger] next];
    self.currentIndex++;
    if (self.currentIndex == self.radioArray.count) {
        self.currentIndex = 0;
    }
    YQRadioListModel *model = self.radioArray[self.currentIndex];
    [self.imageView1 sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
    self.titleLable.text = model.title;
   [ self.playButton setImage:[UIImage imageNamed:@"播放.jpg"] forState:UIControlStateNormal];
}
- (IBAction)didChangeRateAction:(UISlider *)sender {
#warning ---拖动应该有声音呀(解决办法 不添加点击事件 而是 addAction 因为有 事件发生的类型)
    
    CMTime  time = CMTimeMake(sender.value * [YQAVManager shareManger].player.currentItem.duration.value, [YQAVManager shareManger].player.currentItem.duration.timescale);
    [[YQAVManager shareManger] seekToTime:time];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
